define(['routes', 'loader', 'angularAMD', 'ui-bootstrap',
    'angular-sanitize', 'blockUI', 'ui.route'
], function (config, loader, angularAMD) {
    var app = angular.module("webapp", ['ngSanitize',
        'ui.bootstrap', 'ui.router', 'blockUI'
    ]);
    app.provider('router', function ($stateProvider, $urlRouterProvider) {
        var urlCollection;
        this.$get = function ($http, $state) {
            return {
                setUpRoutes: function () {
                    $http.get(urlCollection).success(function (collection) {


                        for (var routeName in collection) {
                            if (!$state.get(routeName)) {
                                // console.log(collection[routeName]);

                                $stateProvider.state(routeName, {
                                    templateUrl: collection[routeName].templateUrl,
                                    url: collection[routeName].url,
                                    resolve: loader(collection[routeName].dependencies),
                                });

                            }
                        }



                    });
                }
            }
        };
        this.setCollectionUrl = function (url) {
            urlCollection = url;
        }
    });
    app.config(function ($stateProvider, $urlRouterProvider, routerProvider) {

        $stateProvider
            .state('home', {
                url: '/home',
                templateUrl: '/OSRS/pages/home.html'
            });

        // 默认路由
        if(config.defaultRoute != undefined) {
            $urlRouterProvider.when("", config.defaultRoute);
        }
         //$urlRouterProvider.otherwise('/home');



        routerProvider.setCollectionUrl('/OSRS/right/init');


        // // 配置路由
        //
        // if (config.routes != undefined) {
        //     angular.forEach(config.routes, function (route, path) {
        //         $stateProvider.state(path, {
        //             templateUrl: route.templateUrl,
        //             url: route.url,
        //             resolve: loader(route.dependencies),
        //             // allowAnonymous: route.allowAnonymous
        //         });
        //     });
        // }

    }).run(function (router) {
        router.setUpRoutes();
    })


    app.controller("root", function ($rootScope, $scope, $state) {
        $rootScope.$state = $state;
        $rootScope.stars0 = ['星', '星', '星', '星', '星'];

        $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
            console.log(toState.name);
            var state = toState.name;
            $("#" + state).addClass('layui-this');
            $(".layui-nav-item").removeClass("layui-nav-itemed");
            $("#" + state).parent().parent().addClass('layui-nav-itemed');
        });


    });


    return angularAMD.bootstrap(app);
});